package com.cy.dbsys.service.impl;

import com.cy.commom.pojo.PageObject;
import com.cy.dbsys.pojo.SysLog;
import com.cy.dbsys.dao.SysLogDao;
import com.cy.dbsys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    /**新增*/
    @Override
    public void saveObject(SysLog entity) {
        sysLogDao.insertObject(entity);
    }
    
    /**查询模块*/
    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent,Integer pageSize) {
        //1、校验参数合法性
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码不正确");
        //2、基于条件查询总记录数
        int rowCount = sysLogDao.getRowCount(username);
        if(rowCount==0)
            throw  new IllegalArgumentException("没有查到对应的记录");
        //3、基于条件查询当前页记录
            //3.1定义页面记录大小
//            int pageSize = 5;
            //3.2计算startIndex大小
            int startIndex =(pageCurrent-1)*pageSize;
            //3.3执行查询操作
            List<SysLog> records = sysLogDao.findPageObjects(username,startIndex,pageSize);
        //4.对分页信息以及当前页记录进行封装
            //4.1构建PageObject对象
            PageObject<SysLog> pageObject=new PageObject<>();
            //4.2封装数据
            pageObject.setPageCurrent(pageCurrent);
            pageObject.setPageSize(pageSize);
            pageObject.setRowCount(rowCount);
            pageObject.setRecords(records);
            pageObject.setPageCount((rowCount-1)/pageSize+1);//?
        //5.返回封装结果。
        return pageObject;

    }
}
